<?php
/**
 * Description
 * User: duanwenjie
 * Date: 2019/6/9
 * Time: 11:30 AM
 */

namespace app\index\controller;

use app\common\model\ToolModel;
use app\common\service\ExcelLibraryService;
use app\common\YujisoController;
use app\index\service\InventoryService;
use think\Exception;

class Inventory extends YujisoController
{
    public $param;
    public function __construct()
    {
        parent::__construct();
        $temp = $this->request->param();
        if (isset($temp['s'])) unset($temp['s']);
        $this->param = $temp;
    }

    /**
     * 获取盘点单列表
     * @author duanwenjie
     */
    public function getList()
    {
        $param = $this->param;
        $result = InventoryService::getList($param);
        returnJson('1','查询成功',$result['list'],$result['total']);
    }


    /**
     * 导出数据
     * @author duanwenjie
     */
    public function export()
    {
        $param = $this->param;
        $res = InventoryService::export($param);
        exitJson($res);
    }


    /**
     * 提审
     * @author duanwenjie
     */
    public function sure()
    {
        $param = $this->param;
        if ($this->request->isPost()){
            $id = $param['id'];
            $data[] = array("id" => $id, "status" => 20);
            if (!empty($id)){
                ToolModel::dbInsertOrUpdate('inventory', $data);
            }
            returnJson(1, "操作成功");
        }
    }

    /**
     * 新增/编辑
     * @author duanwenjie
     */
    public function edit()
    {
        try{
            $param = $this->param;
            if ($this->request->isPost()){
                if (isset($param['type']) && $param['type'] == 3){
                    InventoryService::immediatelySave($param);
                }else{
                    $res = InventoryService::check($param);
                    exitJson($res);
                }
            }else{
                $result = InventoryService::getInventoryDetail($param);
                returnJson(1, "",$result);
            }
        }catch (Exception $e){
            trace($e->getMessage(), 'error');
            throw new Exception($e->getMessage());
        }
    }

    /**
     * 上传文件
     * @author duanwenjie
     */
    public function upload()
    {
        $param = $this->param;
        $param['title'] = ['SKU', '盘点数量']; //必须校验头部
        $param['actionAsync'] = '\app\index\service\InventoryService::uploadHandle'; //上传数据的数据回调到service层处理
        $res = ExcelLibraryService::read($param);
        exitJson($res);
    }

    /**
     * 导入盘点结果保存数据
     * @author duanwenjie
     */
    public function saveData()
    {
        $param = $this->param;
        InventoryService::saveData($param);
    }

    /**
     * 获取盘点单数据
     * @author duanwenjie
     */
    public function getInventoryList()
    {
        $param = $this->param;
        $res = InventoryService::getInventoryList($param);
        returnJson(1, '', $res);

    }

    /**
     * 通过国家获取仓库
     * @author duanwenjie
     */
    public function getWarehouseByCountry()
    {
        $param = $this->param;
        $res = InventoryService::getWarehouseByCountry($param);
        returnJson(1,'',$res);
    }

    /**
     * 生成盘点单
     * @author duanwenjie
     */
    public function saveInventory()
    {
        $param = $this->param;
        InventoryService::saveInventory($param);
    }
}